<!--
 * @Author: daidai
 * @Date: 2022-02-20 16:16:42
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-10-25 09:18:22
 * @FilePath: \web-pc\src\pages\big-screen\view\indexs\left-center.vue
-->
<template>
  <div v-if="pageflag" class="chart-container">
    <Echart id="charts_rlt" :options="options" isClick class="left_center_inner" ref="charts" height="240px"
      @chartClick="chartClick" />
    <!-- 底座背景 -->
    <div class="bg"></div>
  </div>

  <Reacquire v-else @onclick="getData" style="line-height: 200px">
    重新获取
  </Reacquire>
</template>

<script>
import { currentPOST } from 'api/modules';
import { colorsA, colorsB, legend, xAxis, yAxis } from '@/config/EChartVar';
import { graphic } from 'echarts';
import { getPie3D, getParametricEquation } from '@/utils/chart'; //工具类js，页面路径自己修改

export default {
  data() {
    return {
      options: {},
      optionData: [
        {
          name: '五金',
          value: 28,

          itemStyle: {
            color: '#C740F1',
          },
        },
        {
          name: '服装',
          value: 22,
          itemStyle: {
            color: '#6DCDE6',
          },
        },
        {
          name: '食品',
          value: 14,
          itemStyle: {
            color: '#6942F3',
          },
        },
        {
          name: '建材',
          value: 36,
          itemStyle: {
            color: '#3054F3',
          },
        },
      ],
      pageflag: true,
      timer: null,
    };
  },
  created() {
    this.getData();
  },
  mounted() { },
  beforeDestroy() {
    this.clearData();
  },
  methods: {
    clearData() {
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
      }
    },
    getData() {
      this.pageflag = true;

      this.$nextTick(() => {
        this.init();
      });
      // this.switper();
      // this.pageflag = false;
    },
    //轮询
    switper() {
      if (this.timer) {
        return;
      }
      let looper = (a) => {
        this.getData();
      };
      this.timer = setInterval(
        looper,
        2000 // this.$store.state.setting.echartsAutoTime // staticAutoTime
      );
      let myChart = this.$refs.charts.chart;
      myChart.on('mouseover', (params) => {
        this.clearData();
      });
      myChart.on('mouseout', (params) => {
        this.timer = setInterval(
          looper,
          2000 // this.$store.state.setting.echartsAutoTime // staticAutoTime
        );
      });
    },
    init(currentData) {
      let legend = {
        // orient: "horizontal",
        top: '25%',
        left: '85%',
        formatter: function (param) {
          return param;
        },
      };
      // 传入数据生成 option, 构建3d饼状图, 参数工具文件已经备注的很详细
      const options = getPie3D(this.optionData, 0, 320, 25, 16, 0.9, legend);
      // 是否需要label指引线，如果要就添加一个透明的2d饼状图并调整角度使得labelLine和3d的饼状图对齐，并再次setOption
      options.series.push({
        name: '货物品类占比', //自己根据场景修改
        backgroundColor: 'transparent',
        type: 'pie',
        label: {
          show: true,
          opacity: 1,
          fontSize: 13,
          lineHeight: 20,
          formatter: '{b} \n{d}%'
          // formatter: '{b}: {c} ({d}%)' // 自定义标签内容格式
          // show: false // 隐藏标签
        },
        startAngle: -40, // 起始角度，支持范围[0, 360]。
        clockwise: false, // 饼图的扇区是否是顺时针排布。上述这两项配置主要是为了对齐3d的样式
        radius: ['20%', '50%'],
        // center: ['50%', '50%'],
        data: this.optionData,
        itemStyle: {
          opacity: 0, //这里必须是0，不然2d的图会覆盖在表面
        },
      });

      this.options = options;
    },
    chartClick(params) {
      console.log('点击的数据', params);
      if (params.componentSubType == 'pie') {
        // 这里处理你的点击逻辑
        this.$emit('chartClick', params, '类货量', 'RightCenterTop');
      }

    },
  },
};
</script>
<style lang="scss" scoped>
.chart-container {
  position: relative;
  width: 100%;
  height: 100%;

  .left_center_inner,
  .bg {
    margin-left: -50px;
    width: 100%;
    height: 100%;
  }

  .left_center_inner {
    top: -5px;
    z-index: 1;
  }

  .bg {
    position: absolute;
    bottom: 42px;
    left: 51%;
    // z-index: -1;
    width: 270px;
    height: 185px;
    background: no-repeat center;
    background-image: url('../../assets/img/launch/pie_bg.png');
    background-size: 100% 100%;
    transform: translateX(-50%);
  }
}
</style>
